<template>
    <div class="listchoosesize">
        <div class="listbuyimg">
            <div class="buyimg">
                <img :src="this.dataList.listSrc" />
            </div>
            <div class="listbuyprice">
                <div class="price">
                    ¥<span>{{ this.dataList.sale_price }}</span>
                </div>
                <div class="placechoose">请选择尺码 数量</div>
            </div>
        </div>
        <div class="title">尺码</div>
        <div class="closesize" @click="close(0)"></div>
        <ul class="allsize">
            <li
                :style="{
                    color: sizes.flag[index] == 1 ? activeColor : disableColor
                }"
                class="sizeitem"
                :class="activeClass == index ? 'selected' : ''"
                @click="selectSize(index)"
                v-for="(item, index) in sizes.size"
                :key="index"
            >
                {{ item }}
            </li>
        </ul>
        <div class="buynum">
            <span class="buytitle">购买数量</span>
            <van-stepper
                class="stepper"
                style="display: inline;"
                v-model="buyNum"
                name="周数"
                @change="changeNum"
            />
        </div>

        <div class="suresize" data-comid="17802769" @click="addCart">
            确定
        </div>
    </div>
</template>

<script>
import { Toast } from "vant";
import usersApi from "../api/userApi";
import { getUser } from "../utils/auth.js";
export default {
    data() {
        return {
            dataList: {},
            sizes: [],
            userSize: null,
            cartItem: {},
            buyNum: 1,
            activeColor: "#333333",
            disableColor: "#ccc",
            activeClass: -1,
            sizeChecked: 0
        };
    },

    components: {},

    methods: {
        //选择商品码数
        selectSize(index) {
            if (this.sizes.flag[index]) {
                this.activeClass = index;
                this.sizeChecked = 1;
                this.userSize = +this.sizes.size[index];
            }
        },
        close(msg) {
            this.$emit("closeSku", msg);
        },
        changeNum() {},

        addCart() {
            if (this.sizeChecked) {
                if (!JSON.parse(localStorage.getItem("cart"))) {
                    this.cartList = [];
                } else {
                    this.cartList = JSON.parse(localStorage.getItem("cart"));
                }
                this.cartItem.sku = this.dataList.sku;
                this.cartItem.size = this.userSize;
                this.cartItem.price = this.dataList.sale_price;
                this.cartItem.num = this.buyNum;
                this.cartItem.src = this.dataList.listSrc;
                this.cartItem.title = this.dataList.title;
                this.cartItem.goodinfo = this.dataList;
                this.cartItem.isCheck = false;
                this.cartList.forEach((item, index) => {
                    if (
                        item.sku == this.cartItem.sku &&
                        item.size == this.cartItem.size
                    ) {
                        item.num =
                            item.num == undefined ? 1 : item.num + this.buyNum;
                        return false;
                    }
                });
                let isPush = this.cartList.some((item, index) => {
                    return (
                        item.sku == this.cartItem.sku &&
                        item.size == this.cartItem.size
                    );
                });
                if (!isPush) {
                    this.cartList.unshift(this.cartItem);
                }
                // 发请求，存购物车
                usersApi
                    .getuser({ username: getUser() })
                    .then(res => {
                        if (res.data.code) {
                            // console.log(res.data.data[0]._id);
                            // 拿到用户_id,发送请求 修改用户信息
                            usersApi
                                .editCart(res.data.data[0]._id, this.cartList)
                                .then(res => {
                                    // 轻提示
                                })
                                .catch(err => {
                                    console.log("出错了" + err);
                                });
                        }
                    })
                    .catch(err => {
                        console.log("出错了" + err);
                    });

                localStorage.setItem("cart", JSON.stringify(this.cartList));
                let numSum = 0;
                this.cartList.forEach(item => {
                    numSum += item.num;
                });
                this.cartNum = numSum;
                Toast("添加成功");
                this.$emit("closeSku", 0);
                this.$emit("updataNum", this.cartNum);
            }
        }

        //选择商品码数
    },

    created() {
        this.dataList = JSON.parse(localStorage.getItem("sku"));
        this.sizes = JSON.parse(localStorage.getItem("sku")).sizes;
    }
};
</script>

<style scoped lang="scss">
@import "@/assets/sass/common.scss";
@import "@/assets/sass/vw.scss";
.listchoosesize {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    background: #fff;
    z-index: 1111;
    font-size: 16px;
}
.listchoosesize .title {
    margin: 0.9rem 0 0.8rem 0.5rem;
    font-size: 0.7rem;
    color: #333;
}
.listchoosesize .closesize {
    position: absolute;
    right: 0.5rem;
    top: 0.6rem;
    width: 1.3rem;
    height: 1.3rem;
    background: url("http://0.touch.js.al.okbuycdn.com/resources/complete/images/invite/m_close.png")
        center center no-repeat;
    background-size: 0.7rem 0.7rem;
}
.listchoosesize .allsize {
    overflow: hidden;
    margin: 0 0.5rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid #e8e8e8;
}
.listchoosesize .allsize li {
    float: left;
    min-width: 3rem;
    height: 2rem;
    line-height: 2rem;
    margin: 0 0.5rem 0.5rem 0;
    text-align: center;
    font-size: 0.6rem;
    color: #333;
    background: #f4f4f4;
    border-radius: 0.4rem;
}
.listchoosesize .allsize li.nosize {
    color: #ccc;
}

.listchoosesize .suresize {
    width: 100%;
    height: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    font-size: 0.75rem;
    color: #fff;
    background: #d70057;
}
.listchoosesize .nobuyer {
    width: 100%;
    height: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    font-size: 0.75rem;
    color: #fff;
    background: #7f7f7f;
}
.selected {
    color: #fff !important;
    background: #d70057 !important;
}
#formatting {
    position: fixed;
    right: 0.5rem;
    bottom: 2.55rem;
    width: 1.9rem;
    height: 1.9rem;
    background: url("http://0.touch.js.al.okbuycdn.com/resources/complete/images/product/listviewed.png");
    background-size: 100%;
    z-index: 111;
}
#formatting.witch {
    background: url("http://0.touch.js.al.okbuycdn.com/resources/complete/images/product/listview.png");
    background-size: 100%;
}
.nodata {
    padding-bottom: 3.5rem;
    text-align: center;
    font-size: 0.7rem;
    color: #999;
}
.nodata img {
    margin: 5rem auto 0.75rem;
    width: 6rem;
}
.nodata div {
    text-align: center;
}
.buynum {
    overflow: hidden;
    height: 3.45rem;
    line-height: 3.45rem;
    margin: 0 0.5rem;
    border-bottom: 1px solid #e8e8e8;
    position: relative;
}
.buynum .buytitle {
    float: left;
}
.buynum .numin {
    overflow: hidden;
    float: right;
    height: 2.1rem;
    margin-top: 0.7rem;
}
.stepper {
    position: absolute;
    right: 0;
}
.buynum .numin .add,
.buynum .numin .reduce,
.buynum .numin .zpadd,
.buynum .numin .zpreduce {
    width: 2rem;
    height: 2rem;
    line-height: 2rem;
    border: 1px solid #e8e8e8;
    text-align: center;
    font-size: 1rem;
    color: #333;
}
.buynum .numin span {
    float: left;
    display: inline-block;
}
.buynum .numin .numtxt {
    width: 2rem;
    height: 2rem;
    line-height: 2rem;
    border-top: 1px solid #e8e8e8;
    border-bottom: 1px solid #e8e8e8;
    text-align: center;
    font-size: 0.75rem;
    color: #666;
}
.buynum .numin .noadd,
.buynum .numin .noreduce,
.buynum .numin .nozpadd,
.buynum .numin .nozpreduce {
    color: #d8d8d8;
}
.listbuyimg {
    position: relative;
    height: 6rem;
    border-bottom: 1px solid #e8e8e8;
}
.listbuyimg .buyimg {
    position: absolute;
    left: 0.5rem;
    top: -0.5rem;
    width: 5.5rem;
    height: 5.5rem;
    background: #fff;
    border: 1px solid #e8e8e8;
    border-radius: 0.2rem;
}
.listbuyimg .buyimg img {
    width: 5.5rem;
    height: 5.5rem;
}
.listbuyimg .buyimg span {
    position: absolute;
    left: 0.75rem;
    top: 0.75rem;
    display: inline-block;
    width: 4rem;
    height: 4rem;
    line-height: 4rem;
    text-align: center;
    font-size: 0.8rem;
    color: #fff;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 100%;
}
.listbuyimg .listbuyprice {
    padding: 1.5rem 0 0 6.5rem;
}
.listbuyimg .listbuyprice .price {
    font-size: 0.8rem;
    color: #d70057;
}
.listchoose {
    position: relative;
    width: 100%;
    background: #fff;
}
.listchoose .choosehead {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 2.2rem;
    line-height: 2.2rem;
    font-size: 0.6rem;
    color: #333;
    background: #fff;
    z-index: 5;
    border-bottom: 1px solid #e5e5e5;
}
.listchoose .choosehead span {
    float: left;
    display: inline-block;
    width: 3.7rem;
    height: 1.3rem;
    line-height: 1.3rem;
    padding: 0 0.1rem 0 0.2rem;
    margin: 0.45rem 1rem;
    text-align: center;
    background: #f4f4f4;
    border-radius: 0.2rem;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    border: 1px solid #f4f4f4;
}
.listchoose .choosehead span em {
    margin-right: 0.1rem;
}
.listchoose .choosehead span.active {
    color: #d70057;
    border: 1px solid #d70057;
    background: none;
}
.listchoose .choosehead span i {
    display: inline-block;
    width: 0;
    height: 0;
    vertical-align: -0.05rem;
    margin-left: 0.2rem;
    border: 0.2rem solid transparent;
    border-top: 0.2rem solid #8b8b8b;
}
.listchoose .choosehead span.down {
    height: 1.79rem;
    z-index: 111;
    background: #fff;
    border: 1px solid #e5e5e5;
    border-bottom: none;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
}
.listchoose .choosehead span.down i {
    vertical-align: 0.15rem;
    border-bottom: 0.2rem solid #8b8b8b;
    border-top: none;
}
.listchoose .choosebox {
    display: none;
}
.listchoose .choosebody {
    position: absolute;
    top: 2.2rem;
    width: 100%;
    left: 0;
    z-index: 3;
    height: 14rem;
    overflow-y: scroll;
    background: #fff;
    clear: both;
}
.listchoose .choosebody .bodylist {
    display: none;
    overflow: hidden;
}
.listchoose .choosebody .bodylist.active {
    display: block;
}
.listchoose .choosebody .bodylist li {
    overflow: hidden;
    float: left;
    width: 46%;
    height: 2.3rem;
    line-height: 2.3rem;
    padding-left: 4%;
    font-size: 0.65rem;
    color: #333;
}
.listchoose .choosebody .bodylist li.active {
    color: #d70057;
}
.listchoose .choosebtn {
    width: 100%;
    overflow: hidden;
    position: absolute;
    left: 0;
    top: 16rem;
    z-index: 4;
}
.listchoose .choosebtn div {
    float: left;
    width: 50%;
    height: 2.5rem;
    line-height: 2.5rem;
    text-align: center;
    font-size: 0.75rem;
}
.listchoose .choosebtn div.choosereset {
    color: #333;
    background: #fff;
    border-top: 1px solid #e5e5e5;
}
.listchoose .choosebtn div.choosesure {
    color: #fff;
    background: #d70057;
    border-top: 1px solid #d70057;
}
.sortitem,
.topicsortitem {
    margin-bottom: 2.5rem;
}
.search_history {
    height: 100%;
}
</style>
